package
{
	import com.xcopy.FileReader;
	import com.xcopy.Screen;
	import fl.containers.UILoader;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.StageDisplayState;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;

	public class Slot25Core extends Slot25Standard
	{
		// 玩家 class 是用來記錄跟玩家有關的屬性，比如名字，token 等等……
		public function Slot25Core(r:*)
		{
			super(r);
		}
		public var token:String            = '';
		public var ID:String               = '';
		public var SID:String              = '';
		public var background:Number       = 0;
		public var ver:String              = 'X.20111206.01.1';
		//V0.0901.05 	剩餘音效，jackpot 時機，message 時機，today/total 延遲
		//V0.0901.06 	取消 popup 視窗
		//V0.1004.01	更新低frame數的顯示不正常
		//V0.1013.01	架構重整
		//V0.1013.02	叢林女孩 popup...		final result's counter 計算錯誤修正
		//V0.1014.01	叢林女孩 popup 時機...
		//V0.1104.01	文字輸入框
		public var defaultLobby:Number     = 1; // 預設大廳
		public var totalLobbies:Number     = 2; // 目前有幾個廳
		protected var _currentLobby:Number = defaultLobby; // 目前所在大廳
		public var dirMain:String          = 'res/common/game18/main/';
		public var dirRoom:String          = 'res/common/game18/room/';
		public var dirDoll:String          = 'res/common/public/doll/';
		public var TIME_DELAY:Number       = 7.0;
		////	public var downloadDomain:String     = '';
		////	public var downloadDomainBase:String = '';
		////	public var infoInit:Array            = [];
		////	public var infoData:Array            = [];
		public var barname:Array           = ['瘋狂拜金女', '叢林探險'];
		protected var _gold:Number         = 0;
		protected var _avatar:Number       = 0;
		protected var _cloth:Number        = 0;
		protected var _jackpot:Number      = 0;
		protected var _queueAlert:Array    = [];
		protected var _queueMessage:Array  = [];
		protected var _queueToday:Array    = [];
		protected var _queueTotal:Array    = [];
		protected var _quality:Boolean     = true;
		protected var _full:Boolean        = false;
		protected var _sound:Boolean       = true;
		private var ads:Array              = [];
		public var playing:Boolean;

		public function analyseAD(content:String):void
		{
			var temp:Array  = content.split("\n");
			var len:uint    = temp.length;
			var cols:Array  = [];
			var names:Array = [];
			var i:int;
			for (i = 0; i < len; i++)
			{
				if (temp[i].indexOf('below_ad') == 0)
				{
					cols = temp[i].split(" ='");
					ads.push('http://golden.goldengame.com.tw/ad_upfile/' + cols[1].replace("';", ''));
				}
			}
			if (len > 0)
			{
				renderAD(0);
			}
		}

		private function renderAD(index:int):void
		{
			for each (var table:Slot25Table in main.tables)
			{
				table.menu.setAD(ads[index]);
			}
			var len:uint = ads.length;
			index = (index + 1) % len;
			//addCaller(this, {time: 4, count: 1, onUpdate: renderAD, onUpdateParams: [index]});
		}

		public function format(s:String, f:String):String
		{
			var len:Number = f.length - s.length;
			var ret:String = '';
			ret = (len > 0 ? f.substr(0, len) : '') + s;
			return ret;
		}

		public function set currentLobby(n:Number):void
		{
			_currentLobby = n;
			gold = gold;
			jackpot = jackpot;
		}

		public function get currentLobby():Number
		{
			return _currentLobby;
		}

		public function set gold(n:Number):void
		{
			_gold = n;
			this.main.tables[currentLobby].menu.money = _gold;
		}

		public function get gold():Number
		{
			return _gold;
		}

		public function set avatar(n:Number):void
		{
			_avatar = n;
		}

		public function get avatar():Number
		{
			return _avatar;
		}

		public function set cloth(n:Number):void
		{
			_cloth = n;
		}

		public function get cloth():Number
		{
			return _cloth;
		}

		public function set jackpot(n:Number):void
		{
			_jackpot = n;
			this.main.tables[currentLobby].lobby.jackpot = n;
			this.main.tables[currentLobby].machine.jackpot = n;
		}

		public function get jackpot():Number
		{
			return _jackpot;
		}

		public function addToday(n:Number):void
		{
			_queueToday.push(n);
			popToday();
		}

		public function addTotal(n:Number):void
		{
			_queueTotal.push(n);
			popTotal();
		}

		public function addTalking(msg:String):void
		{
			var i:Number = 0;
			for (i = 0; i < totalLobbies; i++)
			{
				this.main.tables[i].menu.addTalking(msg);
			}
		}

		public function delayMessage(msg:String):void
		{
			addCaller(this, {time: TIME_DELAY, count: 1, onUpdate: addMessage, onUpdateParams: [msg]});
		}

		public function addMessage(msg:String):void
		{
			_queueMessage.push(msg);
			popMessage();
		}

		public function delayAward(type:Number, no:Number, ID:String, name:String, msg:String):void
		{
			addCaller(this, {time: TIME_DELAY, count: 1, onUpdate: addAward, onUpdateParams: [type, no, ID, name, msg]});
		}

		public function addAward(type:Number, no:Number, ID:String, name:String, msg:String):void
		{
			var str:String = '';
			//str += '恭喜【' + barname[type] + ' ' + no + '】機台' + "\n";
			//str += '[' + ID + ']' + name + "\n";
			str += msg + "\n";
			//_queueAlert.push(str); // <= 這個暫時先取消，以免彈出式視窗一直打擾玩家進行遊戲
			popAlert();
		}

		public function delayBonus(type:Number, no:Number, ID:String, name:String, btype:Number, point:Number):void
		{
			addCaller(this, {time: TIME_DELAY, count: 1, onUpdate: addBonus, onUpdateParams: [type, no, ID, name, btype, point]});
		}

		public function addBonus(type:Number, no:Number, ID:String, name:String, btype:Number, point:Number):void
		{
			var str:String = '';
			str += '恭喜【' + barname[type] + ' ' + no + '】機台' + "\n";
			str += '[' + ID + ']' + name + "\n";
			str += '獲得彩金 ' + point + "\n";
			popAlert();
		}

		public function delayEvent(type:Number, value:Number):void
		{
			addCaller(this, {time: TIME_DELAY, count: 1, onUpdate: addEvent, onUpdateParams: [type, value]});
		}

		public function addEvent(type:Number, value:Number):void
		{
			var i:Number = 0;
			for (i = 0; i < totalLobbies; i++)
			{
				switch (type)
				{
					case 0:
					case 4:
					{
						if (value == 0) // 秀廣告
						{
							this.main.tables[i].menu.openAD();
						}
						else // 秀積分
						{
							this.main.tables[i].menu.closeAD();
						}
						break;
					}
					case 1:
					case 7:
					{
						//addToday(value);
						addTotal(value);
						break;
					}
					case 2:
					case 8:
					{
						//addTotal(value);
						addToday(value);
						break;
					}
				}
			}
		}

		public function readPic():void
		{
			var i:Number = 0;
			for (i = 0; i < totalLobbies; i++)
			{
				this.main.tables[i].menu.setPic(dirDoll + 'Avatar' + avatar + '_' + cloth + '.swf');
			}
		}

		public function popToday():void
		{
			if (!this.main.tables[_currentLobby].machine.canPost)
			{
				return;
			}
			var i:Number = 0;
			for (i = 0; i < _queueToday.length; i++)
			{
				this.main.tables[_currentLobby].menu.addToday(_queueToday[i]);
			}
			_queueToday = [];
		}

		public function popTotal():void
		{
			if (!this.main.tables[_currentLobby].machine.canPost)
			{
				return;
			}
			var i:Number = 0;
			for (i = 0; i < _queueTotal.length; i++)
			{
				this.main.tables[_currentLobby].menu.addTotal(_queueTotal[i]);
			}
			_queueTotal = [];
		}

		/** 將目前 queue 裡的訊息 queue 到視窗裡 */
		public function popAlert():void
		{
			if (!this.main.tables[_currentLobby].machine.canPost)
			{
				return;
			}
			var i:Number = 0;
			for (i = 0; i < _queueAlert.length; i++)
			{
				this.main.tables[_currentLobby].alert.addAlert(_queueAlert[i]);
			}
			_queueAlert = [];
		}

		public function popMessage():void
		{
			if (!this.main.tables[_currentLobby].machine.canPost)
			{
				return;
			}
			var i:Number = 0;
			var j:Number = 0;
			for (j = 0; j < _queueMessage.length; j++)
			{
				for (i = 0; i < totalLobbies; i++)
				{
					this.main.tables[i].menu.addMessage(_queueMessage[j]);
				}
			}
			_queueMessage = [];
		}

		public function set quality(b:Boolean):void
		{
			_quality = b;
			var i:Number = 0;
			for (i = 0; i < totalLobbies; i++)
			{
				this.main.tables[i].menu.quality = _quality;
			}
			if (_quality)
			{
				this.main.stage.quality = StageQuality.BEST;
			}
			else
			{
				this.main.stage.quality = StageQuality.LOW;
			}
		}

		public function get quality():Boolean
		{
			return _quality;
		}

		public function set full(b:Boolean):void
		{
			_full = b;
			var i:Number = 0;
			for (i = 0; i < totalLobbies; i++)
			{
				this.main.tables[i].menu.full = _full;
			}
			if (_full)
			{
				this.main.stage.displayState = StageDisplayState.FULL_SCREEN;
			}
			else
			{
				this.main.stage.displayState = StageDisplayState.NORMAL;
			}
		}

		public function get full():Boolean
		{
			return _full;
		}

		public function set sound(b:Boolean):void
		{
			_sound = b;
			var i:Number = 0;
			for (i = 0; i < totalLobbies; i++)
			{
				this.main.tables[i].menu.sound = _sound;
				if (_sound)
				{
					this.main.tables[i].radio.adjustMusicVolume(1);
					this.main.tables[i].radio.adjustSoundVolume(1);
				}
				else
				{
					this.main.tables[i].radio.adjustMusicVolume(0);
					this.main.tables[i].radio.adjustSoundVolume(0);
					this.main.tables[i].radio.stopSound('RunBar');
					this.main.tables[i].radio.stopSound('Extra');
				}
			}
		}

		public function get sound():Boolean
		{
			return _sound;
		}

		/*

		AREA=0
		BACKGROUND=0
		BASE=100
		DISCONNECT_COST=0
		GAMENO=0
		GAMESERVERNO=0
		ID0=TS000100
		MACTTYPE=0
		MAXLEVEL=200
		MINLEVEL=0
		MINMONEY=1000
		MODEL0=2
		NAME=
		NEEDPLAYER=1
		NICKNAME0=????
		P1=0
		P2=0
		P3=0
		PLAYER_COUNT=1
		PW=	SEC=6
		SPACIAL=0
		SYSTEMCOST=60
		SYSWAITCOUNT=0
		TABLENO=0
		TAG=0
		TAI=100
		TIMES=0
		VIPONLY=0

		ADMIN=0
		BONUS=2010-02-13
		EXP=2454611
		GGRING=2010-05-10
		GID=2117034
		GPOINT=9434314
		ID=TS000100
		LEVEL=20
		MODEL=2
		NICKNAME=????
		PPOINT=630000
		RICH=2010-08-09
		SRICH=0000-00-00
		TPOINT=71419
		VIP=2010-07-09

		V1=桌號;
		V2=目前在此桌的玩家ID;
		V3=目前在此桌的玩家暱稱;
		V4=MD5;
		V5=座位保留時間;
		V6=被拉幾次Jackpot;
		V7=FreeItem個數;
		V8=圖像編號;

		*/
		public function splitData(info:String):Array
		{
			if (info.substr(info.length - 1) == "\n")
			{
				info = info.substr(0, info.length - 1);
			}
			return info.split("\n");
		}

		public function splitInfo(info:String):Dictionary
		{
			var args:Array     = info.split("\t");
			var arg:Array      = [];
			var ret:Dictionary = new Dictionary(true);
			var i:Number       = 0;
			for (i = 0; i < args.length; i++)
			{
				arg = args[i].split('=');
				ret[arg[0]] = arg[1].replace(' ', '');
			}
			return ret;
		}

		// 解析 server 傳來資料
		public function initGame(SocketID:String, PlayerID:String, Option:String):void
		{
			ID = PlayerID;
			SID = SocketID;
			main.txtInput.visible = true;
			main.timer.start();
		}

		public function initData(Option:String):void
		{
			var infoData:Dictionary = splitInfo(Option);
			gold = Number(infoData['GPOINT']); // 儲存目前所擁有金幣
			avatar = Number(infoData['MODEL']); // 儲存角色娃娃
			cloth = Number(infoData['CLOTHES']); // 儲存角色衣服
			readPic();
		}
	}
}
